package com.ruoyi.project.system.service.impl;

import java.util.*;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.mapper.*;
import com.ruoyi.project.system.service.ICollegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 院校Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@Service
public class CollegeServiceImpl implements ICollegeService
{

    private static final Logger log = LoggerFactory.getLogger(CollegeServiceImpl.class);

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private SysDeptMapper deptMapper;


    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private KnowledgeArticleMapper knowledgeArticleMapper;
    @Autowired
    private CourseStudyMapper courseStudyMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private StudentMapper studentMapper;


    @Autowired
    private CollegeStaffMapper collegeStaffMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private StudentCourseQuestionMapper studentCourseQuestionMapper;




    /**
     * 查询院校
     * 
     * @param collegeId 院校ID
     * @return 院校
     */
    ;@Override
    public College selectCollegeById(Integer collegeId)
    {
        return collegeMapper.selectCollegeById(collegeId);
    }

    /**
     * 查询院校列表
     * 
     * @param college 院校
     * @return 院校
     */
    @Override
    public List<College> selectCollegeList(College college)
    {
        return collegeMapper.selectCollegeList(college);
    }

    /**
     * 新增院校
     * 
     * @param college 院校
     * @return 结果
     */
    @Override
    public int insertCollege(College college)
    {

        int i1 = collegeMapper.insertCollege(college);
        //当前用户的院校id
        College colleges = collegeMapper.selectCollegeByCode(college.getCollegeCode());

        //插入會員表
        Member member = new Member();
        member.setTypeCode(3l);
        List<CollegeStaff> list = collegeStaffMapper.selectCollegeStaffList(new CollegeStaff(){{
            setCollegeId(colleges.getCollegeId().longValue());
        }});
        if(list.size()<10){
            member.setCode(colleges.getCollegeCode()+"00"+list.size()+1);
        }else if(list.size()<100){
            member.setCode(colleges.getCollegeCode()+"0"+list.size()+1);
        }else{
            member.setCode(colleges.getCollegeCode()+list.size()+1);
        }
        member.setPwd("123456");
        member.setCellphone(colleges.getCollegePhone());
        member.setCreateDt(new Date());
        memberMapper.insertMember(member);


        //插入教師表
        List<Member> members = memberMapper.selectMemberList(member);
        CollegeStaff collegeStaff = new CollegeStaff();
        collegeStaff.setMemberId(members.get(0).getId().longValue());
        collegeStaff.setCollegeId(college.getCollegeId().longValue());
        collegeStaff.setCollegeCode(college.getCollegeCode());
        collegeStaff.setCollegeName(college.getCollegeName());
        collegeStaff.setName(college.getCollegeName()+"管理员");
        collegeStaff.setCellphone(colleges.getCollegePhone());


        return  collegeStaffMapper.insertCollegeStaff(collegeStaff);

    }

    /**
     * 导入院校
     *
     * @param college 院校
     * @return 结果
     */

    public int importCollege(College college)
    {

        int i1 = collegeMapper.insertCollege(college);

        return  i1;
    }

    /**
     * 修改院校
     * 
     * @param college 院校
     * @return 结果
     */
    @Override
    public int updateCollege(College college)
    {

            return collegeMapper.updateCollege(college);

    }

    /**
     * 批量删除院校
     * 
     * @param collegeIds 需要删除的院校ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCollegeByIds(Integer[] collegeIds)
    {
        for(Integer collegeId : collegeIds){
            collegeMapper.deleteByCollegeId(collegeId);
        }
        return collegeMapper.deleteCollegeByIds(collegeIds);
    }

    /**
     * 删除院校信息
     * 
     * @param collegeId 院校ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCollegeById(Integer collegeId)
    {
        collegeMapper.deleteByCollegeId(collegeId);
        return collegeMapper.deleteCollegeById(collegeId);
    }


    /**
     * 导入数据
     *
     * @param collegeList 用户数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importCollege(List<College> collegeList, boolean updateSupport) {
        if (StringUtils.isNull(collegeList) || collegeList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (College college : collegeList)
        {
            try
            {
                // 验证是否存在这个院校名称
                College u = collegeMapper.selectCollegeByCollegeName(college.getCollegeName());
                if (StringUtils.isNull(u))
                {
                    college.setCollegeCreateDt(new Date());
                    this.importCollege(college);

                    College colleges = collegeMapper.selectCollegeByCode(college.getCollegeCode());


                    //插入會員表
                    Member member = new Member();
                    member.setTypeCode(3l);
                    List<CollegeStaff> list = collegeStaffMapper.selectCollegeStaffList(new CollegeStaff(){{
                        setCollegeId(colleges.getCollegeId().longValue());
                    }});
                    if(list.size()<10){
                        member.setCode(colleges.getCollegeCode()+"00"+list.size()+1);
                    }else if(list.size()<100){
                        member.setCode(colleges.getCollegeCode()+"0"+list.size()+1);
                    }else{
                        member.setCode(colleges.getCollegeCode()+list.size()+1);
                    }
                    member.setPwd("123456");
                    member.setCellphone(colleges.getCollegePhone());
                    member.setCreateDt(new Date());
                    memberMapper.insertMember(member);


                    //插入教師表
                    List<Member> members = memberMapper.selectMemberList(member);
                    CollegeStaff collegeStaff = new CollegeStaff();
                    collegeStaff.setMemberId(members.get(0).getId().longValue());
                    collegeStaff.setCollegeId(college.getCollegeId().longValue());
                    collegeStaff.setCollegeCode(college.getCollegeCode());
                    collegeStaff.setCollegeName(college.getCollegeName());
                    collegeStaff.setName(college.getCollegeName());
                    collegeStaff.setCellphone(colleges.getCollegePhone());


                    collegeStaffMapper.insertCollegeStaff(collegeStaff);



                    successNum++;
                    successMsg.append("<br/>" + successNum + "、院校 " + college.getCollegeName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    college.setCollegeModifyDt(new Date());
                    this.updateCollege(college);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、院校 " + college.getCollegeName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、院校 " + college.getCollegeName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、院校 " + college.getCollegeName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public College selectCollegeByCollegeName(String collegeName) {
        return collegeMapper.selectCollegeByCollegeName(collegeName);
    }

    @Override
    public int getStudyList() {
        //查询学生做题数量
        StudentCourseQuestion studentCourseQuestion = new StudentCourseQuestion();

        int sunRead = studentCourseQuestionMapper.selectStudentCourseQuestionList(studentCourseQuestion).size();

        //查询学生安全课程数量
        List<CourseStudy> courseStudies = courseStudyMapper.selectCourseStudyList(new CourseStudy());

        return sunRead+courseStudies.size();
    }


    @Override
    public Map<String, Object> getSumStudyNum() {
        Map<String,Object> map = new HashMap<>();
        //1,先获取课程学习总次数
        int sum = courseMapper.selectSumCount();
        map.put("sumCount",sum);
        List<String> courseNameList = courseMapper.selectCourseName();
        map.put("courseNameList",courseNameList);
        List<Integer> courseCountList = courseMapper.selectCourseCount();
        map.put("courseCountList",courseCountList);
        return map;
    }

    /**
     * 获取学校数量
     * @return
     */
    @Override
    public int getSchoolList() {

        return studentMapper.getSchoolList();
    }

    @Override
    public int getStudentList() {
        return studentMapper.getStudentList();
    }

    @Override
    public int getStudentListCollegeId(Long deptId) {
        return studentMapper.getStudentListCollegeId(deptId);
    }

    @Override
    public int getStudyListCollegeId(Long deptId) {

        //查询学生做题数量
        StudentCourseQuestion studentCourseQuestion = new StudentCourseQuestion();
        studentCourseQuestion.setCollegeId(deptId.intValue());
        int sunRead = studentCourseQuestionMapper.selectStudentCourseQuestionList(studentCourseQuestion).size();

        //查询学生安全课程数量
        CourseStudy courseStudy = new CourseStudy();
        courseStudy.setCollegeId(deptId);
        List<CourseStudy> courseStudies = courseStudyMapper.selectCourseStudyList(courseStudy);

        return sunRead+courseStudies.size();
    }

    /**
     *  获取每日学习次数图表里面所需要的值  加学校限制
     * @param deptId
     * @return
     */

    @Override
    public Map<String, Object> getSumStudyNumCollegeId(Long deptId) {
        Map<String,Object> map = new HashMap<>();
        //1,先获取课程学习总次数
        int sum = courseMapper.selectSumCountCollegeId(deptId);
        map.put("sumCount",sum);
        List<String> courseNameList = courseMapper.selectCourseNameCollegeId(deptId);
        map.put("courseNameList",courseNameList);
        List<Integer> courseCountList = courseMapper.selectCourseCountCollegeId(deptId);
        map.put("courseCountList",courseCountList);
        return map;
    }


}
